import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.Random;

/**
 * @Author: zhenzhong.wang@honeywell.com
 * @Date: 2018/11/8 9:51 PM
 */
public class PanguCustTest {

  public static void main(String[] args) {
    for (int i = 0; i < 100; i ++) {
      Thread thread = new Thread(new Runnable() {
        @Override
        public void run() {
          runCustService(false);
        }
      });
      thread.start();
    }
  }
  public static void runCustService(boolean allTheTime) {
    if (allTheTime) {
      while (true) {
        getCustDetail();
      }
    } else {
      getCustDetail();
    }
  }


  public static void getCustDetail() {

    String url = "http://ipangu.baidu.com/ipangu-callout/cust/custDetail_init.action";
//    String custId = "153632" + "1402025619920";
    String custId = "153632" + generateRandomNum(13);

    String result = "";
    BufferedReader in = null;
    try {
      String urlNameString = url + "?custId=" + custId;
      URL realUrl = new URL(urlNameString);
      // 打开和URL之间的连接
      URLConnection connection = realUrl.openConnection();
      // 设置通用的请求属性
      connection.setRequestProperty("accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8");
      connection.setRequestProperty("connection", "Keep-Alive");
      connection.setRequestProperty("Upgrade-Insecure-Requests", "1");
      connection.setRequestProperty("user-agent",
          "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.77 Safari/537.36");
      connection.setRequestProperty("Host", "ipangu.baidu.com");
      connection.setRequestProperty("Cookie", "BAIDUID=2C85FB20934C476FA37D23CF53E851C3:FG=1; BIDUPSID=2C85FB20934C476FA37D23CF53E851C3; PSTM=1538920397; BDUSS=5rdkVjOTNJc0t-UzhqNUc0cXRHMnZxTWcyem9NLW5-NS12NmczazFZTDI3djliQVFBQUFBJCQAAAAAAAAAAAEAAAA8s4420bDDzrXEvNO3xsOoAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAPZh2Fv2YdhbZE; Hm_lvt_bdc3f84f9a2236f657905fe9446ad004=1541682007; SIGNIN_UC=70a2711cf1d3d9b1a82d2f87d633bd8a02916956233; uc_login_unique=d5b7131dbd5cd080c453eb3d80ab4190; uc_recom_mark=cmVjb21tYXJrXzE3OTQ1ODI5; __cas__st__155=045d4094e86b5770f7ee4db093e4bcd219467b2a82d687ae998ba87bb70c106327904d1b9c25f20cb1fbbd17; __cas__id__155=17945829; __cas__rn__=291695623; UCID_COOKIE_NAME=17945829; H_SF_JTZY_KKS=1; Hm_lpvt_bdc3f84f9a2236f657905fe9446ad004=1541683682; JSESSIONID=C553372060823B38D1366A0A1E35BBD9");
      connection.setRequestProperty("Accept-Language", "zh-CN,zh;q=0.9,en;q=0.8");

      // 建立实际的连接
      connection.connect();
//      // 获取所有响应头字段
//      Map<String, List<String>> map = connection.getHeaderFields();
//      // 遍历所有的响应头字段
//      for (String key : map.keySet()) {
//        System.out.println(key + "--->" + map.get(key));
//      }
      // 定义 BufferedReader输入流来读取URL的响应
      in = new BufferedReader(new InputStreamReader(
          connection.getInputStream()));
      String line;
      while ((line = in.readLine()) != null) {
        result += line + "\n";
      }
      if (result.indexOf("客户详情") > 0) {
        System.out.println("有效ID：" + custId);
      }
//      else
//        System.out.println("无效ID：" + custId);
//      System.out.println(result);
    } catch (Exception e) {
      System.out.println("发送GET请求出现异常！" + e);
      e.printStackTrace();
    }
    // 使用finally块来关闭输入流
    finally {
      try {
        if (in != null) {
          in.close();
        }
      } catch (Exception e2) {
        e2.printStackTrace();
      }
    }
  }

  public static String generateRandomNum(int n) {
    Random random = new Random();
    String randomNum = "";
    for (int i = 0; i < n; i++) {
      randomNum += random.nextInt(10);
    }
    return randomNum;
  }
}